package com.ng.desktop.expansionsa.view.alquiler;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.ng.desktop.expansionsa.model.Alquiler;
import com.ng.desktop.expansionsa.model.EstadoAlquiler;
import com.ng.desktop.expansionsa.persistence.AlquilerPersistence;
import com.ng.desktop.expansionsa.services.NGFactoryBean;
import com.ng.framework.persistence.NGSearchParameter;
import com.ng.framework.persistence.NGSearchParameter.BinaryOperation;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.NGABMCFrame;
import com.ng.framework.view.desktop.NGGUIUtils;
import com.ng.framework.view.desktop.NGTablePanel;
import com.ng.framework.view.desktop.filters.NGFilter;
import com.ng.framework.view.desktop.filters.NGJComboBoxFilter;

public class AlquilerFrame extends NGABMCFrame<Alquiler, AlquilerPersistence> {

	private static final long serialVersionUID = -9011834645135943977L;
	
	private FilterNotificacion filterNotificacion;
	
	public AlquilerFrame() {
		this.setTitle("Gestión de Alquileres");
		this.setFrameIcon(NGGUIUtils.getImageIcon("icons/receipt-invoice.png"));
		this.setSize(new Dimension(1021, 600));
		this.centrar();
	}

	@Override
	protected AlquilerPersistence createPersistence() {
		return (AlquilerPersistence) NGFactoryBean.getInstance().getBean("alquilerPersistence");
	}

	@SuppressWarnings("serial")
	@Override
	protected List<NGFilter> createFilters() {
		List<NGFilter> filters = new ArrayList<NGFilter>();
		
		filters.add(new FilterCliente());
		filters.add(new NGJComboBoxFilter<EstadoAlquiler>("Estado", Alquiler.class, "estadoAlquiler", Type.STRING, UnaryOperation.EQUALS, Arrays.asList(EstadoAlquiler.values())) {

			@Override
			protected String getComponentValue(JComboBox component) {
				String componentValue = null;
				
				EstadoAlquiler estadoAlquiler = (EstadoAlquiler)component.getSelectedItem();
				if(estadoAlquiler != null) {
					componentValue = estadoAlquiler.name();
				}
				
				return componentValue;
			}
		});
		filters.add(this.getFilterNotificacion());
		
		return filters;
	}
	
	private FilterNotificacion getFilterNotificacion() {
		if(filterNotificacion == null) {
			this.filterNotificacion = new FilterNotificacion();
		}
		
		return filterNotificacion;
	}

	@Override
	protected NGTablePanel<Alquiler, AlquilerPersistence> createTable(
			AlquilerPersistence persistence) {
		
		return new AlquilerTablePanel(persistence, this.getFilterNotificacion());
	}
	
	private class FilterCliente extends NGFilter {
		
		private static final long serialVersionUID = -5108351349224993989L;

		private JTextField textField;

		public FilterCliente() {
			super("Cliente");
			
			this.buildGui();
		}
		
		private void buildGui() {
			this.setLayout(new GridBagLayout());
			
			this.add(this.getTextField(), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		}
		
		protected JTextField getTextField() {
			if(textField == null) {
				this.textField = new JTextField();
			}
			return textField;
		}

		@Override
		public List<NGSearchParameter> getSearchParameters() {
			List<NGSearchParameter> ngSearchParameters = null;
			
			if(StringUtils.isNotBlank(this.getTextField().getText())) {
				NGSearchParameter razonSocial = new NGSearchParameter(Alquiler.class, "cliente", this.getTextField().getText(), Type.STRING, UnaryOperation.LIKE, true, "razonSocial");
				NGSearchParameter nombre = new NGSearchParameter(Alquiler.class, "cliente", this.getTextField().getText(), Type.STRING, UnaryOperation.LIKE, true, "nombre");
				NGSearchParameter apellido = new NGSearchParameter(Alquiler.class, "cliente", this.getTextField().getText(), Type.STRING, UnaryOperation.LIKE, true, "apellido");
				NGSearchParameter nombreApellido = new NGSearchParameter(nombre, apellido, BinaryOperation.OR);
				
				ngSearchParameters = Arrays.asList(new NGSearchParameter(razonSocial, nombreApellido, BinaryOperation.OR));
			}
			
			return ngSearchParameters;
		}

		@Override
		public void clear() {
			getTextField().setText(null);	
		}
		
	}

}
